﻿using System.Management;


namespace Xbrowser
{
    public partial class PrintSetting : Form
    {
        public string DefaultPrintName { get;set;}

        public PrintSetting()
        {
            InitializeComponent();
            GetPrinterList();
        }

        //获取操作系统默认打印机名称
        public void GetPrinterList()
        {
            //获取所有打印机信息
            string query = string.Format("SELECT * from Win32_Printer ");
            var searcher = new ManagementObjectSearcher(query);
            var printers = searcher.Get();
            foreach (var printer in printers)
            {
                string printName = printer.Properties["DeviceID"].Value.ToString();
                listPrinter.Items.Add(printName);

                if (bool.Parse(printer.Properties["Default"].Value.ToString()) == true)
                {
                    listPrinter.SelectedItem = printName;
                    DefaultPrintName = printName;
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            DefaultPrintName = listPrinter.SelectedItem.ToString();
            this.Close();
        }
    }
}
